﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JaernCloud.Language.Compiler.Ast;
using JaernCloud.Language.Compiler.Types.Builtin;

namespace JaernCloud.Language.Compiler.Types {
    
    /// <summary>
    /// Base class for all NodeTypes. Implements default inheritance behavior.
    /// </summary>
    public abstract class NodeType : INodeType {
        
        private string _name = null;
        private List<string> _names = null;
        private List<INodeType> _children = null;
        private List<INodeType> _inheritsFrom = null;
        private INodeType _parent = null;
        private NodeIdentifierScope _identifierScope = null;

        public string Name {
            get {
                if (_name == null) {
                    if (SourceNode.Tag != null) {
                        _name = SourceNode.Tag.Name;
                    } else {
                        _name = String.Empty;
                    }
                }
                return _name;
            }
        }

        public NodeIdentifierScope IdentifierScope {
            get {
                if (_identifierScope == null) {
                    _identifierScope = new NodeIdentifierScope(this);
                }
                return _identifierScope;
            }
        }

        public List<string> Names {
            get {
                if (_names == null) {
                    _names = new List<string>();
                    if (!String.IsNullOrEmpty(Name)) {
                        _names.Add(Name);
                        foreach (INodeType inheritsFrom in InheritsFrom) {
                            _names.Add(inheritsFrom.Name);
                        }
                    }
                }
                return _names;
            }
        }

        public List<INodeType> Children {
            get {
                if (_children == null) {
                    _children = new List<INodeType>();
                    foreach (AstNode childAst in SourceNode.Children) {
                        INodeType child = NodeTypeGenerator.GetNodeTypeInstanceFor(childAst);
                        child.Parent = this;
                        _children.Add(child);
                    }
                    // also get children from inherited nodes
                    foreach (INodeType inheritsFrom in InheritsFrom) {
                        _children.AddRange(inheritsFrom.Children);
                    }
                }
                return _children;
            }
        }

        public List<INodeType> InheritsFrom {
            get {
                if (_inheritsFrom == null) {
                    _inheritsFrom = new List<INodeType>();
                    foreach (AstInheritsFrom inheritsFromAst in SourceNode.InheritsFrom) {
                        if (inheritsFromAst.TagSelector != null) {
                            // non-constant inheritsfrom (constants are used for the values collection)
                            IList<INodeType> inheritsFrom = IdentifierScope.Lookup(inheritsFromAst.TagSelector);
                            if (inheritsFrom == null) {
                                throw new UnknownIdentifierException(inheritsFromAst.TagSelector, this.SourceNode);
                            }
                            _inheritsFrom.AddRange(inheritsFrom);
                        }
                    }
                    if (SourceNode.Query != null) {
                        QueryNodeType queryNodeType = new QueryNodeType(this);
                        _inheritsFrom.Add(queryNodeType);
                    }
                }
                return _inheritsFrom;
            }
        }

        public INodeType Parent {
            get { return _parent; }
            set { _parent = value; }
        }

        public override string ToString() {
            return Name;
        }

        public AstNode SourceNode { get; set; }

        public abstract AstTag GetRequiredAstTag();

        public abstract INodeType GetNodeTypeInstanceFor(AstNode node);

        #region IEquatable

        public virtual bool Equals(INodeType other) {
            // TODO: Equals could be more exact and be based on a cached hashcode
            if (!Names.Except(other.Names).Any()) {
                // no difference in names
                var mine = Children;
                var others = other.Children;
                if (mine.Count == others.Count) {
                    for (int i = 0; i < mine.Count; i++) {
                        if (!mine[i].Equals(others[i])) {
                            return false;
                        }
                    }
                    // no difference in children
                    return true;
                }
            }
            return false;
        }

        #endregion

    }
}
